package com.accp.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.accp.entity.TzIndexImg;
import com.accp.entity.TzProd;
import com.accp.service.ITzIndexImgService;
import com.accp.service.ITzProdService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author 辉
 * @since 2019-12-26
 */
@RestController
@RequestMapping("/accp/tzProd")
public class TzProdController {

	@Autowired
	ITzProdService itzProdService;
	@Autowired
	ITzIndexImgService itzIndexImgService;
	
	/**
	 *  得到首页导航栏显示的数据
	*  @param categoryId 分类id
	 * @return
	 */
	@RequestMapping("/getIndexNav")
	public Map<String,Object>  getIndexNav(){
		Map<String,Object> map = new HashMap<>();
		
		QueryWrapper<TzIndexImg> qwIndexImg = new QueryWrapper<>();
		List<TzIndexImg> indexImgList = itzIndexImgService.page(new Page(1,3),qwIndexImg.orderByDesc("upload_time")).getRecords(); //轮播商品
		
		IPage<TzProd> page = new Page<>(0, 8);
		QueryWrapper<TzProd> qwProd = new QueryWrapper<TzProd>();
		qwProd.orderByDesc("putaway_time");
		List<TzProd> prodList = itzProdService.page(page,qwProd).getRecords();//最新商品
		
		map.put("indexImgList",indexImgList); //轮播商品
		map.put("prodNavList",prodList);		//最新商品
		return map;
	}
	
	/**
	 * 首页其他商品
	 * @param curPage 当前页码
	 * @param categoryId 分类id
	 * @return
	 */	
	@RequestMapping("/getIndexOther")
	public Map<String, Object> getIndexOther(Integer curPage,Integer categoryId){
		Map<String, Object> map = new HashMap<String, Object>();
		if(curPage==null||curPage<=0) {
			curPage=1;
		}
		IPage<TzProd> page = new Page<>(curPage, 6);
		QueryWrapper<TzProd> qwProd = new QueryWrapper<TzProd>(); 
		if(categoryId!=null&&categoryId!=0) {
			qwProd.eq("category_id", categoryId);
		}
		qwProd.orderByDesc("putaway_time");		
		List<TzProd> prodList = itzProdService.page(page,qwProd ).getRecords();//最新商品
		map.put("pages", page.getPages());
		map.put("prodList", prodList);
		return map;
	}
	
	/**
	 * 根据条件获得  多条件另外查询
	 * */
	@RequestMapping("/getCondition")
	public List<TzProd> getCondition(String c,String v){
		return itzProdService.list(new QueryWrapper<TzProd>().eq(c, v));
	}
	
	
}

